package huxibianjie.com.lbscandroid.ui.old.fragment.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.canyinghao.caneffect.CanShadowDrawable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import huxibianjie.com.lbscandroid.R;
import huxibianjie.com.lbscandroid.manager.UserManager;
import huxibianjie.com.lbscandroid.model.CountDailyModel;
import huxibianjie.com.lbscandroid.model.ItemModel;
import huxibianjie.com.lbscandroid.model.RankingsModel;
import huxibianjie.com.lbscandroid.model.StepModel;
import huxibianjie.com.lbscandroid.model.UserModel;
import huxibianjie.com.lbscandroid.pedomemter.CustomBarChart;


/**
 * 首页
 * Created by CZF on 2018/7/25.
 */

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.BaseViewHolder> {
    private List<ItemModel> dataList = new ArrayList<>();
    private Activity activity;
    private boolean isLoadChart;

    public HomeAdapter(Activity activity) {
        this.activity = activity;
    }

    public void replaceAll(@Nullable List<ItemModel> list) {
        isLoadChart = true;
        dataList.clear();
        if (list != null && list.size() > 0) {
            dataList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void addAll(@Nullable List<ItemModel> list) {
        if (list != null && list.size() > 0) {
            int start = this.dataList.size();
            this.dataList.addAll(list);
            notifyItemRangeInserted(start, list.size());
        }
    }


    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case ItemModel.Type.ITEM_HOME_TOP:
                return new TopViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_top, parent, false));
            case ItemModel.Type.ITEM_HOME_TYPE:
                return new TypeViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_type, parent, false));
            case ItemModel.Type.ITEM_HOME_TITLE:
                return new TitleViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_title, parent, false));
            case ItemModel.Type.ITEM_HOME_INFO:
                return new InfoViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_info, parent, false));
            case ItemModel.Type.ITEM_HOME_CHART:
                return new ChartViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_chart, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        if (dataList.get(position).type == ItemModel.Type.ITEM_HOME_INFO) {
            holder.setData(dataList.get(position).data, position);
        } else {
            holder.setData(dataList.get(position).data);
        }
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return dataList.get(position).type;
    }


    class BaseViewHolder extends RecyclerView.ViewHolder {

        BaseViewHolder(View itemView) {
            super(itemView);
        }

        public void setData(Object data) {
        }

        public void setData(Object data, int position) {
        }
    }

    class TopViewHolder extends BaseViewHolder {
        private LinearLayout layMyMoney;
        private LinearLayout layCoin;
        private TextView tvMyMoney, tvCurrentCoin, tvTodayStep, tvDailyLimit, tvLocationDigCoin, tvTaskDigCoin, tvTodayDigCoin;
        private TextView tvBtn;

        TopViewHolder(View itemView) {
            super(itemView);
            layMyMoney = itemView.findViewById(R.id.lay_my_money);
            layCoin = itemView.findViewById(R.id.lay_coin);
            tvMyMoney = itemView.findViewById(R.id.tv_my_money);
            tvCurrentCoin = itemView.findViewById(R.id.tv_current_coin);
            tvTodayStep = itemView.findViewById(R.id.tv_today_step);
            tvDailyLimit = itemView.findViewById(R.id.tv_daily_limit);
            tvLocationDigCoin = itemView.findViewById(R.id.tv_location_dig_coin);
            tvTaskDigCoin = itemView.findViewById(R.id.tv_task_dig_coin);
            tvTodayDigCoin = itemView.findViewById(R.id.tv_today_dig_coin);
            tvBtn = itemView.findViewById(R.id.tv_btn);

            CanShadowDrawable.Builder.on(layMyMoney)
                    .radius(activity.getResources().getDimension(R.dimen.x4))
                    .shadowColor(Color.parseColor("#40000000"))
                    .shadowRange(activity.getResources().getDimension(R.dimen.x8))
                    .offsetTop(activity.getResources().getDimension(R.dimen.x8))
                    .offsetBottom(activity.getResources().getDimension(R.dimen.x8))
                    .offsetLeft(activity.getResources().getDimension(R.dimen.x8))
                    .offsetRight(activity.getResources().getDimension(R.dimen.x8))
                    .create();
            CanShadowDrawable.Builder.on(layCoin)
                    .radius(activity.getResources().getDimension(R.dimen.x4))
                    .shadowColor(Color.parseColor("#40000000"))
                    .shadowRange(activity.getResources().getDimension(R.dimen.x8))
                    .offsetTop(activity.getResources().getDimension(R.dimen.x8))
                    .offsetBottom(activity.getResources().getDimension(R.dimen.x8))
                    .offsetLeft(activity.getResources().getDimension(R.dimen.x8))
                    .offsetRight(activity.getResources().getDimension(R.dimen.x8))
                    .create();

            tvBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (itemClick != null) {
                        itemClick.toTurn();
                    }
                }
            });
        }

        private StepModel stepModel;

        public void setData(Object data) {
            if (UserManager.getInstance().isLogin()) {
                if (data instanceof StepModel) {
                    stepModel = (StepModel) data;
                    tvCurrentCoin.setText(String.valueOf(stepModel.getStep()));
                    tvTodayStep.setText(String.valueOf(stepModel.getSumStep()));
                }
                UserModel userModel = UserManager.getInstance().getUserModel();

                tvMyMoney.setText(String.valueOf(userModel.getMoney()));
                tvDailyLimit.setText(String.valueOf(10000));
                tvLocationDigCoin.setText(String.valueOf(userModel.getTodayCurrency()));
                tvTaskDigCoin.setText(String.valueOf(userModel.getTaskCurrency()));
                tvTodayDigCoin.setText(String.valueOf(userModel.getTaskCurrency() + userModel.getTodayCurrency()));
            } else {
                tvMyMoney.setText(String.valueOf(0));
                tvCurrentCoin.setText(String.valueOf(0));
                tvTodayStep.setText(String.valueOf(0));
                tvDailyLimit.setText(String.valueOf(10000));
                tvLocationDigCoin.setText(String.valueOf(0));
                tvTaskDigCoin.setText(String.valueOf(0));
                tvTodayDigCoin.setText(String.valueOf(0));
            }
        }
    }

    class TypeViewHolder extends BaseViewHolder implements View.OnClickListener {
        private LinearLayout layInfo;
        private ImageView ivTaskCoin, ivVideoCoin;
        private RelativeLayout layInfo1, layInfo2;

        TypeViewHolder(View itemView) {
            super(itemView);
            layInfo = itemView.findViewById(R.id.lay_info);

            ivTaskCoin = itemView.findViewById(R.id.iv_task_coin);
            ivVideoCoin = itemView.findViewById(R.id.iv_video_coin);
            layInfo1 = itemView.findViewById(R.id.lay_info1);
            layInfo2 = itemView.findViewById(R.id.lay_info2);

            CanShadowDrawable.Builder.on(layInfo)
                    .radius(activity.getResources().getDimension(R.dimen.x4))
                    .shadowColor(Color.parseColor("#40000000"))
                    .shadowRange(activity.getResources().getDimension(R.dimen.x8))
                    .offsetTop(activity.getResources().getDimension(R.dimen.x8))
                    .offsetBottom(activity.getResources().getDimension(R.dimen.x8))
                    .offsetLeft(activity.getResources().getDimension(R.dimen.x8))
                    .offsetRight(activity.getResources().getDimension(R.dimen.x8))
                    .create();
            ivTaskCoin.setOnClickListener(this);
            ivVideoCoin.setOnClickListener(this);
            layInfo1.setOnClickListener(this);
            layInfo2.setOnClickListener(this);
        }

        public void setData(Object data) {

        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.iv_task_coin:
                    Toast.makeText(activity, "敬请期待", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.iv_video_coin:
                    Toast.makeText(activity, "敬请期待", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.lay_info1:
                    break;
                case R.id.lay_info2:
                    break;
            }
        }
    }

    class TitleViewHolder extends BaseViewHolder {
        private LinearLayout layTitle;

        TitleViewHolder(View itemView) {
            super(itemView);
            layTitle = itemView.findViewById(R.id.lay_title);
//            CanShadowDrawable.Builder.on(layTitle)
//                    .shadowColor(Color.parseColor("#40000000"))
//                    .shadowRange(activity.getResources().getDimension(R.dimen.x8))
//                    .offsetTop(activity.getResources().getDimension(R.dimen.x8))
//                    .offsetLeft(activity.getResources().getDimension(R.dimen.x8))
//                    .offsetRight(activity.getResources().getDimension(R.dimen.x8))
//                    .create();
        }

        public void setData(Object data) {
        }
    }

    class InfoViewHolder extends BaseViewHolder {
        private LinearLayout layItem;
        private TextView tvNumber, tvPhone, tvCoin;

        InfoViewHolder(View itemView) {
            super(itemView);
            layItem = itemView.findViewById(R.id.lay_item);
            tvNumber = itemView.findViewById(R.id.tv_number);
            tvPhone = itemView.findViewById(R.id.tv_phone);
            tvCoin = itemView.findViewById(R.id.tv_coin);
        }

        private RankingsModel.RankingsBean rankingsBean;

        public void setData(Object data, int position) {
            position = position - 2;
            if (data instanceof RankingsModel.RankingsBean) {
                rankingsBean = (RankingsModel.RankingsBean) data;
                tvNumber.setText(String.valueOf(position));
                tvPhone.setText(String.valueOf(rankingsBean.getPhone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2")));
                tvCoin.setText(String.valueOf(rankingsBean.getStep()));
                layItem.setSelected(position < 4);
            }
        }
    }

    class ChartViewHolder extends BaseViewHolder {
        private int[] chartData;
        private String[] xLabel;
        private String[] yLabel;

        private LinearLayout layTitle;

        ChartViewHolder(View itemView) {
            super(itemView);
            layTitle = itemView.findViewById(R.id.lay_title);
        }

        private void loadChart() {
            if (isLoadChart) {
                if (arrayList != null) {
//                arrayList.add(new CountDailyModel(15000, "2018-07-26"));
//                arrayList.add(new CountDailyModel(6500, "2018-07-25"));
//                arrayList.add(new CountDailyModel(3460, "2018-07-24"));
//                arrayList.add(new CountDailyModel(4000, "2018-07-23"));
//                arrayList.add(new CountDailyModel(5000, "2018-07-22"));
//                arrayList.add(new CountDailyModel(1000, "2018-07-21"));
//                arrayList.add(new CountDailyModel(2000, "2018-07-20"));
                    int day = 7;
                    xLabel = new String[day + 1];
                    for (int i = 0; i < xLabel.length; i++) {
                        xLabel[i] = "";
                    }
                    chartData = new int[day + 1];
                    Collections.reverse(arrayList);
                    while (arrayList.size() > day) {
                        arrayList.remove(0);
                    }
                    for (int i = 0; i < arrayList.size(); i++) {
                        chartData[i + 1] = arrayList.get(i).getNumber();
                        xLabel[i + 1] = arrayList.get(i).getTime().substring(5);
                    }

                    yLabel = new String[]{"0", "2000", "4000", "6000", "8000", "10000", "12000", "14000", "16000", "18000"};
                    List<int[]> data = new ArrayList<>();
                    data.add(chartData);
                    List<Integer> color = new ArrayList<>();
                    color.add(R.color.main_color);
                    color.add(R.color.main_grey);
                    layTitle.addView(new CustomBarChart(activity, xLabel, yLabel, data, color));
                }
                isLoadChart = false;
            }
        }

        private List<CountDailyModel> arrayList;

        public void setData(Object data) {
            if (data instanceof List) {
                arrayList = (List<CountDailyModel>) data;
                loadChart();
            }
        }
    }

    private OnItemClick itemClick;

    public void setItemClick(OnItemClick itemClick) {
        this.itemClick = itemClick;
    }

    public interface OnItemClick {
        void toTurn();
    }
}
